function trel = poly2trellis(regNum, g)
  g = g(:);
  coef = de2bi(oct2dec(g));
  numInputSymbols = 2;
  numnumOutputSymbols = 2^length(g);
  numStates = 2^regNum;
  nextStates = genNextStates(numStates);
  outputs = genOutputs(numStates, coef);
  trel = tlist(['trellis','numInputSymbols','numOutputSymbols','numStates','nextStates','outputs'], ...
                numInputSymbols, numnumOutputSymbols, numStates, nextStates, outputs...
              );
endfunction

function nextStates = genNextStates(numStates)
  errStr = 'numStates must an integer';
  if length(numStates) ~= 1
    error(errStr);
  end
  if typeof(numStates) ~= 'constant'
    error(errStr)
  end
  if int(numStates) ~= numStates
    error(errStr)
  end
  
  curStates = 0:(numStates-1);
  nextStates = zeros(numStates,2);
  nextStates(:, 1) = int(curStates(:)/2);
  nextStates(:, 2) = nextStates(:, 1) + numStates/2;
endfunction

function outputs = genOutputs(numStates, coef)
  curStates = 0:(numStates-1);
  curStates = curStates(:);
  outputs = zeros(numStates, 2); // 2 is input symbol number
  coef = coef'
  bitNum = size(coef,1);
  temp = modulo(de2bi(curStates, bitNum)*coef, 2);
  outputs(:,1) = bi2de(temp);
  temp = modulo(de2bi(curStates+numStates)*coef, 2);
  outputs(:,2) = bi2de(temp);
endfunction
